<?php
//ajaxcart1.php
//Update shopping cart/Genus Listings when click "Add Genera" button from Compendium Listings in Compendium search page
//Written by Hongjie Qu
//Last Updated: August 2009
?>
</html>
<SCRIPT LANGUAGE="JavaScript">

function OnDivScroll()
{
	var lstCollegeNames = document.getElementById("G");
	
	if (lstCollegeNames.options.length > 63)
	{
		lstCollegeNames.size=lstCollegeNames.options.length;
	}
	else
	{
		lstCollegeNames.size=63;	
	}	
}

	//On focus of Selectbox
	function OnSelectFocus()
	{
		
		
		if (document.getElementById("divCollegeNames").scrollLeft != 0)
		{
			document.getElementById("divCollegeNames").scrollLeft = 0;
		}


		var lstCollegeNames = document.getElementById('G');
		
		if( lstCollegeNames.options.length > 63)
		{
			lstCollegeNames.focus();
			lstCollegeNames.size=63;
		}
}

</script>
<?php



$debug=0;
$dbhost = "localhost";
$dbuser = "fossilplot";
$dbpass = "fossilplot";
$dbname = "fossilplot";
//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
//Select Database
mysql_select_db($dbname) or die(mysql_error());

$arrGen = $_POST['arrGen'];
//$arrCart = $_POST['arrCart'];
$cartId=$_POST['cartId'];
//$Make = $_POST['Make'];


//if($debug){
//echo "hello selected Gen ".$arrGen."<br>";
//echo "<br><br>hello cartId ".$cartId."<br>";

//echo "hello Cart ".str_replace('*','+',$arrCart)."<br>";
//}


//$exploded_cart = explode(',', $arrCart);

$exploded_gen = explode(',', $arrGen);


if($cartId =="No Items Selected"){
	if($arrGen) 	$cartId=$arrGen;
}
else{
	if($arrGen){
		$tmpCartId=",".$cartId.","; //there is an " " in the beginning of $cartId
				
		for($i=0;$i<sizeof($exploded_gen);$i++){
			$tmp=", ".$exploded_gen[$i].",";
			//echo "tmp is $tmp<br>";
			$pos=strpos($tmpCartId,$tmp);
			if($pos===false) {$cartId =$cartId.", ".$exploded_gen[$i];}

		}
	}


}
//echo "<br><br>new cartis is $cartId";
$groupId="(".$cartId.")";





if($debug){
echo "exploded gen 3 is ".$exploded_gen[0]."<br>";
echo "size of selected gen is ".sizeof($exploded_gen)."<br>";
}
ob_flush(); flush();
if($cartId!="No Items Selected") $len=sizeof(explode(",",$cartId));
else $len=0;
echo "<div id='countdiv'>
          
         
            <font size=2  color='993333'>$len genera are selected.</font>
 </div>";

//echo "<input type=\"hidden\" id=\"showphy\" name=\"showphy\" value='$Make'/>";
//echo "<input type=\"hidden\" id=\"showord\" name=\"showord\" value='$arrOrder'/>";
if ($cartId=="No Items Selected"){
	
		echo "<select name=\"G[]\" multiple size=\"63\" style=\"WIDTH: 290px\">";
		echo "<option>"."No Items Selected"."</option>";
		echo "</select>";
	
	
}
else{ //$arrGen is not null
//echo "cart is $exploded_cart[0]";

//echo "<div id=\"select_container\" style=\"overflow-x:scroll; width:290px; overflow: -moz-scrollbars-horizontal;\" >";
//echo "<select name=\"G[]\" multiple size=\"63\">";

echo "<div id=\"divCollegeNames\" style=\"OVERFLOW: auto;WIDTH: 304px;HEIGHT: 1007px\" onscroll=\"OnDivScroll();\">";
echo "<SELECT id=\"G\" name=\"G[]\" size=\"63\" multiple=\"\" onfocus=\"OnSelectFocus();\">";


$result = mysql_query("select id, genus_name, phylum_name, class_name, order_name,fad_date,lad_date from genus where id in $groupId");

while ($row = mysql_fetch_array($result)) {
	echo "<option>".$row{'genus_name'}."; ".$row{'phylum_name'}."; ".$row{'class_name'}.";".$row{'order_name'}."; ".$row{'fad_date'}."; ".$row{'lad_date'}."; ".$row{'id'}."</option>";
}




echo "</select>";

echo "</div>";
} //end of else ($arrGen is not null)


?>
</html>